Skip to content

fix(resharding): use replica_session role and disable FK validation - #1540

Merged
meskill merged 3 commits into
mainfrom
meskill-2026-09-14-test-resharding---yry
Sep 15, 2026
Merged

meskill merged 3 commits into
mainfrom
meskill-2026-09-14-test-resharding---yry

Conversation

@meskill

@meskill meskill commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Handle the FK constraints during resharding process:

When we copy tables in parallel we make the snapshots of tables at the specific moment of time and because of this we can have tables on destination with different WALs (that's why we save lsn for tables after copy) and that's could make data inconsistent for example for FK and other cross-table constraints.

The error could happen in both cases: when we do schema sync for the constraints (they may fail on apply), or during the replication (that may fail because part of the transaction could be already in some table snapshot, but not another table snapshot).

To resolve this:

  1. Use SET session_replication_role=replica on destination in order to disable rules and triggers during the update. That requires additional specific GRANT or superuser, so an error handling for this with a little explanation was added.
  2. Add NOT VALID for FK during replication, so the FK won't be triggered when added (the role doesn't disable it automatically).

This basically disables this validations to the sake of assumption that we have consistent data on source and we do the proper copy that maintains the consistency so during the replication we'll eventually reach the consistent state.

@meskill
meskill force-pushed the meskill-2026-09-14-test-resharding---yry branch from 7808268 to 245b83e Compare September 14, 2026 12:09
@codecov

codecov Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.09639% with 132 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pgdog/src/backend/replication/tests.rs 76.11% 129 Missing ⚠️
pgdog/src/backend/pool/pool_impl.rs 81.81% 2 Missing ⚠️
.../src/backend/replication/logical/subscriber/mod.rs 95.23% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@meskill
meskill force-pushed the meskill-2026-09-14-test-resharding---yry branch from 245b83e to 3a03717 Compare September 14, 2026 15:13
@meskill
meskill force-pushed the meskill-2026-09-14-test-resharding---yry branch from 3a03717 to d84260b Compare September 14, 2026 15:43
@meskill meskill changed the title test(resharding): FK constraints triggered during replication fix(resharding): use replica_session role and disable FK validation Sep 14, 2026
@meskill
meskill force-pushed the meskill-2026-09-14-test-resharding---yry branch from c7722dc to a09fed1 Compare September 14, 2026 22:43
@meskill
meskill requested a review from levkk September 14, 2026 22:55

@levkk levkk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! Thank you 🙏

@meskill
meskill force-pushed the meskill-2026-09-14-test-resharding---yry branch from 0b305d6 to 1910ee7 Compare September 15, 2026 12:14
@meskill

meskill commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

Added check for destination pg versions to avoid possible issues with FK NOT VALID for partitioned tables since it's only supported from pg18. See https://www.postgresql.org/docs/17/sql-altertable.html#SQL-ALTERTABLE-DESC-ADD-TABLE-CONSTRAINT and https://www.postgresql.org/docs/18/release-18.html#RELEASE-18-CONSTRAINTS
for partitioned that will fallback to the old FK to not break the whole resharding

@meskill
meskill merged commit 87bca54 into main Sep 15, 2026
29 checks passed
@meskill
meskill deleted the meskill-2026-09-14-test-resharding---yry branch September 15, 2026 12:42
pull Bot pushed a commit to TheTechOddBug/pgdog that referenced this pull request Sep 29, 2026
Add a new PostDataValidation phase for schema_sync that allows to run
the validation after copying the data. Previously in
pgdogdev#1540 the FK constraints where
added with `NOT VALID` option marking the flag `convalidated = false`
for the related table, which disables the check on creation for existing
data and maybe disables some optimization that could be done by db for
this table.

New step runs `VALIDATE CONSTRAINT` on this FK to make sure the
`convalidated` is set to true on table and the db actually will start to
think the values are validated. Like this since it should not block the
table from actual writes comparing with creating the FK without options,
so we can run this in parallel to our replication process.

The check is manual for now and later PRs will use them to run during
validation.

For the remainder of the flow:
- we copy tables with COPY command
- we create FK constraints with `NOT VALID` since the copied data could
be not in sync because different tables were copied at different
snapshot time
- we replicate changes - eventually, data "should" become synchronized
and valid
- the `PostDataValidation` could be run after the replication or after
some time during the replication to see if there any errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants